<template>
  <div>
    <common-head title="用户登录"/>
    <van-form @submit="onSubmit">
      <van-field
        v-model="user.username"
        name="username"
        label="用户名"
        placeholder="用户名/手机/邮箱"
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        v-model="user.pwd"
        type="password"
        name="pwd"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px;">
        <van-button round block type="danger" native-type="submit">提交</van-button>
      </div>
    </van-form>
    <div class="tips">
      <span class="tip" @click="toRegister">立即注册>></span>
      <span class="tip">忘记密码?</span>
    </div>
    <div class="login-path">
      <div>
        <span class="login-title">使用合作伙伴账号登录:</span>
      </div>
      <div>
        <a href="https://m.360kad.com/Login/SinaLogin?ReturnUrl=https%3a%2f%2fm.360kad.com%2f">
        <img src="https://res.360kad.com/theme/mobile/img/xinlang.png" alt="图片失效了" class="login-img">
        </a>
        <a href="https://m.360kad.com/Login/QzoneLogin?ReturnUrl=https%3a%2f%2fm.360kad.com%2f">
        <img src="https://res.360kad.com/theme/mobile/img/qq.png" alt="图片失效了" class="login-img">
        </a>
      </div>
    </div>
    <div class="login-desc">
      <span class="login-agreement">登录即代表您同意<a class="agreement">《康爱多隐私政策》</a></span>
    </div>
    <div class="tohomo">
      <div class="espide"><span>不想登录？</span></div>
      <van-button type="info" to="/home" size="small" round>去首页逛逛</van-button>
    </div>
  </div>
</template>

<script>
import CommonHead from '_components/CommonHead.vue'
import { doLogin } from '_api'
export default {
  name: 'Login',
  data () {
    return {
      user: {
        username: '',
        pwd: '',
        deviceId: '10',
        deviceName: 'Android'
      }
    }
  },
  methods: {
    onSubmit () {
      doLogin(this.user).then(res => {
        if (res.data.code === 0) {
          // 将token缓存
          localStorage.setItem('token', res.data.data.token)
          // 调用toast弹窗
          this.$toast.success({
            message: '登录成功',
            duration: 1000,
            // 弹窗关闭的回调
            onClose: () => {
              // 如果存在路由就返回前页
              if (this.$route.query.from) {
                this.$router.replace(this.$route.query.from).catch((err) => {
                  console.log(err)
                })
              } else {
                // 否则回到首页
                this.$router.replace('/home')
              }
            }
          })
        }
      })
    },
    // 点击注册
    toRegister () {
      this.$router.push('/register')
    }
  },
  components: {
    CommonHead
  }
}
</script>

<style lang="scss" scoped>
.van-form{
  padding: 10px 0 0 0;
}
.tips{
  padding: 0 10px;
  display: flex;
  justify-content: space-between;
  .tip{
    color: #1264ba;
    line-height: 0.2667rem;
    font-size: 14px;
  }
}
.login-path{
  padding: 0 10px 20px 10px;
  .login-title{
    font-size: 14px;
  }
  .login-img{
    border-radius: 25px;
    overflow: hidden;
    width: 1.2rem;
    height: 1.2rem;
    margin: 5px 10px;
  }
}
.login-desc{
  line-height: 0.4rem;
  .login-agreement{
    padding: 0px 10px 15px 10px;
    font-size: 14px;
    .agreement{
      color: #3e9ffd;
    }
  }
}
.tohomo{
  margin-top: 0.3rem;
  padding: 0px 10px 15px 10px;
  .espide{
    font-size: 0.5rem;
  }
  .van-button{
    margin-left: 1rem;
    text-align: center;
  }
}

</style>
